Skip to main content
The gdal2Coordinates collection provides utilities for converting between different coordinate systems based on the geospatial information embedded in raster files.

Overview

These scripts read the coordinate system and geotransformation matrix from a GDAL-supported raster file to perform coordinate transformations. All conversions use the projection information from the input image.

Available Scripts

pixel2longlat.py

Converts pixel coordinates to latitude/longitude (geographic) coordinates.
sample
float
required
The pixel/sample position (column number)
line
float
required
The line position (row number)
infile
string
required
Path to the input raster file
Usage:
python pixel2longlat.py sample line infile
Example:
python pixel2longlat.py 100 200 terrain.tif
Output:
pixel: 100          line: 200
longitude: -112.456789      latitude: 35.123456
longitude: 112d27'24.44"W   latitude: 35d7'24.44"N
The script calculates coordinates for the center of the specified pixel by applying a half-pixel shift.

pixel2meters.py

Converts pixel coordinates to projected coordinates in meters (or feet, depending on the projection).
sample
float
required
The pixel/sample position (column number)
line
float
required
The line position (row number)
infile
string
required
Path to the input raster file
Usage:
python pixel2meters.py sample line infile
Example:
python pixel2meters.py 500 750 projected_dem.tif
Output:
pixel: 500          line: 750
X: 445678.500000    Y: 3789234.500000

longlat2meters.py

Converts longitude/latitude coordinates to projected coordinates (X, Y) in meters or feet.
longitude
float
required
Longitude in decimal degrees
latitude
float
required
Latitude in decimal degrees
infile
string
required
Path to the input raster file (provides projection information)
Usage:
python longlat2meters.py longitude latitude infile
Example:
python longlat2meters.py -112.5 35.2 projected_map.tif
Output:
longitude: -112.500000      latitude: 35.200000
X: 450000.000000            Y: 3900000.000000

meters2longlat.py

Converts projected coordinates (X, Y) in meters or feet to longitude/latitude.
X
float
required
X coordinate in meters (or feet)
Y
float
required
Y coordinate in meters (or feet)
infile
string
required
Path to the input raster file (provides projection information)
Usage:
python meters2longlat.py X Y infile
Example:
python meters2longlat.py 450000 3900000 projected_map.tif
Output:
X: 450000.000000            Y: 3900000.000000
longitude: -112.500000      latitude: 35.200000
longitude: 112d30'0.00"W    latitude: 35d12'0.00"N

Implementation Details

Coordinate Transformation Process

All scripts follow a similar workflow:
1

Open the input raster

Use GDAL to open the raster file in read-only mode
2

Read geotransformation matrix

Extract the affine transformation coefficients that map between pixel and ground coordinates
3

Create spatial reference objects

Build OGR spatial reference objects from the raster’s projection information
4

Transform coordinates

Apply the appropriate transformation using GDAL/OGR coordinate transformation functions
5

Apply pixel center offset (where applicable)

Shift coordinates to represent the center of pixels rather than corners

Geotransformation Matrix

GDAL uses a 6-coefficient affine transformation:
X = geomatrix[0] + geomatrix[1] * pixel + geomatrix[2] * line
Y = geomatrix[3] + geomatrix[4] * pixel + geomatrix[5] * line
Where:
  • geomatrix[0]: Top-left X coordinate
  • geomatrix[1]: Pixel width (X resolution)
  • geomatrix[2]: Row rotation (typically 0)
  • geomatrix[3]: Top-left Y coordinate
  • geomatrix[4]: Column rotation (typically 0)
  • geomatrix[5]: Pixel height (Y resolution, typically negative)

Requirements

pip install gdal
The scripts support both old and new GDAL Python bindings:
  • Modern: from osgeo import gdal, osr
  • Legacy: import gdal (automatically detected)

Use Cases

Interactive Mapping

Convert mouse clicks on images to geographic coordinates for display

Data Integration

Transform coordinates between datasets with different projections

Ground Control

Identify ground coordinates for surveyed pixel locations

Quality Control

Verify georeferencing accuracy at known control points

Credits

Based on tolatlong.py by Andrey Kiselev (dron@remotesensing.org). Extended for meter/feet conversions by Trent Hare (USGS).
These scripts are part of the GDAL Python samples and are released under the MIT License.